<?php

/*
 * Profile editing page
 */
require_once('rb.php');
R::setup('mysql:host=ftp.giemiebier.nl:3306;dbname=personify', 'school', 'admin123');

R::debug(true);

$return = array();

if (isset($_POST['id'])) {
    $count = R::count('profile', ' userid = ?', array($_POST['id']));

    $profile = R::dispense("profile");
    $profile->userid = $_POST['id'];
    $profile->name = $_POST['name'];
    $profile->lastname = $_POST['lastname'];
    $profile->birthdate = $_POST['birthdate'];
    $profile->address = $_POST['address'];
    $profile->zipcode = $_POST['zipcode'];
    $profile->city = $_POST['city'];
    $profile->countryid = $_POST['countryid'];
    $profile->religion = $_POST['religion'];
    $profile->sex = $_POST['sex'];

    if ($count > 0) {
        $cursor = R::getAll('SELECT id FROM profile WHERE userid = :id', array(':id'=>$_POST['id']));
        foreach($cursor as $c){
            $id = $c['id'];
        }
        $profile->id = $id;
    } 
    R::store($profile);
    $return['valid'] = true;
            
} else {
    $return['error'] = "No id has been sent.";
    $return['valid'] = false;
}
echo json_encode($return);
?>
